{"componentChunkName":"component---src-templates-blog-template-js","path":"/blog/could-not-create-an-instance-of-type-cosmosdb","result":{"data":{"markdownRemark":{"html":"<p>When upserting a new entity to a Cosmosdb Database I was getting </p>\n<pre>\nCould not create an instance of type MyNamespace.IUser. \nType is an interface or abstract class and cannot be instantiated. \nPath 'User.Name', line 1, position 213.\n</pre>\n<p>When using the CosmosDb nuget package you can't specify a custom serialiser as you can with, say, Newtonsoft.</p>\n<p>Instead you can create a constructor which takes a concrete implementation of the interface</p>\n<pre><code>[JsonConstructor]\npublic MyEntity(Status status, SomeOtherType value, User user) : base(user)\n{\n        Status = status;\n        Value = value;\n}\n</code></pre>\n<p>where <code>base(user)</code> calls</p>\n<pre><code>public CosmosEntityBase(IUser user)\n{\n        User = user;\n}\npublic IUser User { get; set; }     \n</code></pre>\n<p>This will be less straightforward if you have multiple implementations of the interface. You'll have to determine which one to instantiate.</p>\n<p>However if the <code>JsonConstructor</code> has the interface <code>IUser</code> in its signature then </p>\n<pre>\nCould not create an instance of type MyNamespace.IUser. Type is an interface or abstract class and cannot be instantiated. Path 'User.Name', line 1, position 213.\n</pre>\n<p>will be returned so that is what you need to avoid.</p>","frontmatter":{"date":"April 23, 2021","slug":"/blog/could-not-create-an-instance-of-type-cosmosdb","title":"Could not create an instance of type (CosmosDb upsert)"}}},"pageContext":{"slug":"/blog/could-not-create-an-instance-of-type-cosmosdb"}},"staticQueryHashes":["3649515864"]}